package stark.app.function.shortcut;

import android.app.Activity;
import android.content.pm.PackageInfo;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.view.View;

import com.blankj.utilcode.util.AppUtils;
import com.blankj.utilcode.util.ImageUtils;
import com.blankj.utilcode.util.SpanUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.bumptech.glide.Glide;
import com.stark.picselect.PhotoSelectorBuilder;
import com.stark.picselect.config.PhotoConfig;
import com.stark.picselect.entity.SelectMediaEntity;
import com.stark.picselect.interfaces.OnSelectResultListener;
import com.stark.shortcut.ShortcutHelper;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.core.graphics.drawable.IconCompat;
import stark.app.R;
import stark.app.databinding.DialogCreateShortcutBinding;
import stark.common.basic.base.BaseNoModelDialog;

/**
 * Author：daizhongbin on 2021/8/23
 * Email：1027154338@qq.com
 * Desc:
 */
public class ShortcutCreateDialog extends BaseNoModelDialog<DialogCreateShortcutBinding> {

    private PackageInfo packageInfo;
    private Activity mActivity;

    public void setPackageInfo(PackageInfo packageInfo) {
        if (packageInfo == null) return;
        this.packageInfo = packageInfo;
        if (mDataBinding == null) return;
        mDataBinding.ivImg.setImageDrawable(AppUtils.getAppIcon(packageInfo.packageName));
        mDataBinding.etName.setText(AppUtils.getAppName(packageInfo.packageName));
    }

    public ShortcutCreateDialog(@NonNull Activity activity) {
        super(activity);
        mActivity = activity;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.dialog_create_shortcut;
    }

    @Override
    protected void initView(View view) {
        setPackageInfo(packageInfo);
        configTip();
        mDataBinding.ivImg.setImageDrawable(AppUtils.getAppIcon(packageInfo.packageName));
        mDataBinding.etName.setText(AppUtils.getAppName(packageInfo.packageName));
        mDataBinding.ivImg.setOnClickListener(v -> {
            clickImg();
        });
        mDataBinding.btnCancel.setOnClickListener(v -> {
            dismiss();
        });
        mDataBinding.btnCreate.setOnClickListener(v -> {
            clickCreate();
        });
    }

    private void clickCreate() {
        //name
        String name = mDataBinding.etName.getText().toString();
        if (TextUtils.isEmpty(name)) {
            mDataBinding.etName.requestFocus();
            ToastUtils.showShort("请输入快捷方式的名字");
            return;
        }
        //icon
        Drawable drawable = mDataBinding.ivImg.getDrawable();
        IconCompat iconCompat = IconCompat.createWithBitmap(ImageUtils.drawable2Bitmap(drawable));
        //创建快捷方式
        ShortcutHelper.installShortCut(name, iconCompat, packageInfo.packageName);

        dismiss();
    }

    private void clickImg() {
        PhotoSelectorBuilder.builder(mActivity)
                .mode(PhotoConfig.Mode.PHOTO)
                .maxCount(1)
                .listener(new OnSelectResultListener<List<SelectMediaEntity>>() {
                    @Override
                    public void onResult(List<SelectMediaEntity> data) {
                        if (data == null) return;
                        SelectMediaEntity entity = data.get(0);
                        Glide.with(mDataBinding.ivImg).load(entity.getUri()).into(mDataBinding.ivImg);
                    }
                });
    }

    private void configTip() {
        SpanUtils.with(mDataBinding.tvTip)
                .append("提示：若无法创建，请先点击去")
                .append("获取桌面快捷方式权限")
                .setClickSpan(Color.parseColor("#FF0000"), true, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        AppUtils.launchAppDetailsSettings();
                    }
                })
                .create();
    }

}
